<template>
  <div class="wrapper">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="search">
          <span>姓名:</span
          ><el-input v-model="name" placeholder="请输入姓名"></el-input>
          <el-button type="primary" @click="check">查询</el-button>
          <el-button type="primary" @click="add">新增</el-button>
        </div>
      </div>
      <div>
        <el-button @click="backtabel">返回</el-button>
      </div>
      <div class="tableform">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column prop="username" label="编号" width="180">
          </el-table-column>
          <el-table-column prop="name" label="姓名" width="180">
          </el-table-column>
          <el-table-column prop="address" label="地址"> </el-table-column>
          <el-table-column prop="phone" label="手机号" width="180">
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click="edit(scope.row)" type="text" size="small"
                >编辑</el-button
              >
              <el-button type="text" size="small" @click="deleteUser(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="block">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage3"
          :page-size="page_size"
          layout="prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="ruleForm.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              v-model="ruleForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="ruleForm.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="支付宝" prop="airpay">
            <el-input v-model="ruleForm.airpay" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="ruleForm.address" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <el-dialog title="提示" :visible.sync="dialogVisible2" width="30%">
        <el-form
          :model="userinfo"
          status-icon
          :rules="rules"
          ref="userinfo"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="userinfo.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="userinfo.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              v-model="userinfo.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="userinfo.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="支付宝" prop="airpay">
            <el-input v-model="userinfo.airpay" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="userinfo.address" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm2('userinfo')"
              >提交</el-button
            >
            <el-button @click="cancelForm2">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { log } from "util";
export default {
  components: {},
  props: {},
  data() {
    return {
      page_size: 8,
      total: 0,
      currentPage3: 1,
      name: "",
      userinfo: {},
      totalData: [],
      tableData: [],
      dialogVisible: false,
      dialogVisible2: false,
      ruleForm: {
        username: "",
        name: "",
        phone: "",
        airpay: "",
        pass: "",
        address: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        airpay: [{ required: true, message: "请输入支付宝", trigger: "blur" }],
        pass: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    check() {
      console.log(this.name);
      var name = this.name;
      this.tableData = [];
      this.totalData.forEach(item => {
        if (item.name == name) {
          this.tableData.push(item);
        }
      });
    },
    add() {
      this.dialogVisible = true;
      console.log("添加新成员");
    },
    edit(data) {
      console.log(data);
      this.userinfo = data;
      this.dialogVisible2 = true;
    },
    deleteUser(data) {
      console.log(data);
      var id = data.username;
      this.$confirm("您确定要删除该用户信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.totalData.forEach((item, index) => {
            if (item.username == id) {
              this.totalData.splice(index, 1);
            }
          });
          this.tabelData();
          return this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.ruleForm);
          this.totalData.unshift(this.ruleForm);
          this.dialogVisible = false;
          this.tabelData();
          return this.$message({
            message: "添加新成员成功",
            type: "sucess"
          });
        } else {
          return this.$message({
            message: "请您填写准确的信息",
            type: "warning"
          });
        }
      });
    },
    cancelForm() {
      this.ruleForm = {
        username: "",
        name: "",
        phone: "",
        airpay: "",
        pass: ""
      };
      this.dialogVisible = false;
      this.$message("您已取消添加新成员");
    },
    submitForm2(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.ruleForm);
          this.dialogVisible2 = false;
        } else {
          return this.$message({
            message: "请您填写准确的信息",
            type: "warning"
          });
        }
      });
    },
    cancelForm2() {
      this.userinfo = {};
      this.dialogVisible2 = false;
      this.$message("您已取消编辑");
    },
    tabelData() {
      this.tableData = this.totalData.slice(0, this.page_size);
      this.total = this.totalData.length;
    },
    handleCurrentChange(val) {
      console.log(val);
      this.tableData = this.totalData.slice(
        this.page_size * (val - 1),
        this.page_size * val
      );
    },
    backtabel() {
      this.tableData = this.totalData.slice(0, this.page_size);
      this.name = "";
    },
    async getTotalData() {
      await this.$http.get("../../static/json/zhubo.json").then(res => {
        console.log(res.data);
        res.data.forEach(item => {
          this.totalData.push(item);
        });
      });
      this.tabelData();
    }
  },
  created() {
    this.getTotalData();
  },
  mounted() {}
};
</script>
<style lang="scss" scoped>
.search {
  font-size: 16px;
  .el-input {
    width: 200px;
    margin: 0 100px 0 20px;
  }
}
</style>
